$(document).ready(function(e){
	createPageTab();
	createActivityListTab();
	readyAllBrand("brandSelect", null);
	findList(1);
});

var totalPageNum = 0;
var isDesc = "desc";
var list = null;
var curPageNum = null;
function findList(pageNow){
	var tmp2=root+"/admin/activity-admin!findList";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":pageNow, "isDesc": isDesc, "brand.id":$("#brandSelect").val(), "page.numberPerPage": 20},
		dataType:"json",
		success: function(json){
			if(list!=null) createActivityListTab();
			totalPageNum = json.totalPageNum;
			list = json.pageData;
			curPageNum = json.curPageNum;
			//写入系统list
			var htmlStr = '';
			for(var i=0; i<json.pageData.length; i++){
				htmlStr += '<tr><td height="24" bgcolor="#FFFFFF"><div align="center" class="STYLE1">';
				htmlStr += '<input name="checkbox" type="checkbox" class="STYLE2" value="'+json.pageData[i].id+'" />';
				htmlStr += '</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF" class="STYLE2">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+((json.curPageNum-1)*20+(i+1))+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].subject+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].brand.name+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].content+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].createDate.replace("T"," ")+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].startDate.replace("T"," ")+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].endDate.replace("T"," ")+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1"><a href="'+root+'/admin/activity-market!goJsp?activity.content='+json.pageData[i].content+'">活动门店</a></div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1"><a href="'+root+'/admin/activity-img!goJsp?activity.content='+json.pageData[i].content+'">生动化管理</a></div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center">';
				htmlStr += '<img src="'+root+'/admin/company/images/037.gif" width="9" height="9" />';
				htmlStr += '<span class="STYLE1"> [</span><a href="javascript:;" onclick="showUpdate('+json.curPageNum+','+i+')">编辑</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center"><span class="STYLE2">';
				htmlStr += '<img src="'+root+'/admin/company/images/010.gif" width="9" height="9" />';
				htmlStr += '</span><span class="STYLE1">[</span><a href="javascript:;" onclick="del('+json.pageData[i].id+')">删除</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td></tr>';
			}
			$("#listInfoTab").append(htmlStr);
			
			//写入分页信息
			$("#pageInfo").html('共'+json.totalCount+'条纪录，当前第'+json.curPageNum+'/'+json.totalPageNum+'页，每页'+json.numberPerPage+'条纪录');
			$("#onePage").unbind("click");
			$("#onePage").bind("click",function(){findList(1);});
			if(json.curPageNum>1){
				$("#perPage").unbind("click");
				$("#perPage").bind("click",function(){findList((json.curPageNum-1));});
			}
			if((json.curPageNum+1)<=json.totalPageNum){
				$("#nextPage").unbind("click");
				$("#nextPage").bind("click",function(){findList((json.curPageNum+1));});
			}
			$("#lastPage").unbind("click");
			$("#lastPage").bind("click",function(){findList(json.totalPageNum);});
		}
	});
}

function showAdd(){
	readyAllBrand("brandSelect_add", null);
	$("#backGroud").show();
	$("#addDiv").show();
}

function closeAdd(){
	$("#backGroud").hide();
	$("#addDiv").hide();
}

function add(){
	if($("#subject_add").val()=="" || $("#subject_add").val()==null){
		alert("主题不能为空！");
		return;
	}
	if($("#content_add").val()=="" || $("#content_add").val()==""){
		alert("内容不能为空！");
		return;
	}
	if($("#startDate_add").val()!=$("#startDate_add").val()){
		alert("开始时间不能为空！");
		return;
	}
	if($("#endDate_add").val()=="" || $("#endDate_add").val()==""){
		alert("结束时间不能为空！");
		return;
	}
	$("#addForm").submit();
}

function del(id){
	var ids = "";
	if(id==null){
		ids = getCheckBoxVal();
		if(ids==""){
			alert("请至少选择一条数据！");
			return;
		}
	}
	var tmp2=root+"/admin/activity-admin!delete";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"activity.id":id, "ids":ids, "page.curPageNum":curPageNum, "isDesc": isDesc, "brand.id":$("#brandSelect").val(), "page.numberPerPage": 20},
		dataType:"json",
		success: function(json){
			$("#ck").removeAttr("checked");
			findList(json.curPageNum);
		}
	});
}

function showUpdate(pageNow, index){
	readyAllBrand("brandSelect_u", null);
	$("#brandSelect_u").val(list[index].brand.id);
	
	$("#activityId_u").val(list[index].id);
	$("#subject_u").val(list[index].subject);
	$("#content_u").val(list[index].content);
	$("#startDate_u").val(list[index].startDate);
	$("#endDate_u").val(list[index].endDate);
	
	$("#backGroud").show();
	$("#updateDiv").show();
	
	$("#updateButton").bind("click",function(){update(pageNow, index);});
}

function closeUpdate(){
	$("#updateForm")[0].reset();
	$("#updateButton").unbind("click");
	$("#backGroud").hide();
	$("#updateDiv").hide();
}

function update(pageNow, index){
	if($("#subject_u").val()=="" || $("#subject_u").val()==null){
		alert("主题不能为空！");
		return;
	}
	if($("#content_u").val()=="" || $("#content_u").val()==""){
		alert("内容不能为空！");
		return;
	}
	if($("#startDate_u").val()!=$("#startDate_u").val()){
		alert("开始时间不能为空！");
		return;
	}
	if($("#endDate_u").val()=="" || $("#endDate_u").val()==""){
		alert("结束时间不能为空！");
		return;
	}
	var tmp2=root+"/admin/activity-admin!update";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"activity.id":$("#activityId_u").val(), "activity.subject": $("#subject_u").val(), "activity.content":$("#content_u").val(),
				"activity.startDate":$("#startDate_u").val(), "activity.endDate":$("#endDate_u").val(), "brand.id":$("#brandSelect_u").val()},
		dataType:"json",
		success: function(json){
			closeUpdate();
			if(json.code=="n"){
				alert("修改失败！");
			}else{
				findList(json.curPageNum);
			}
		}
	});
}

function goPage(){
	var p = $("#pageNum").val();
	if(goPage_v(p)){
		findList(p);
	}
}